package cn.xshi.sys.web;

import cn.xshi.common.annotation.Auth;
import cn.xshi.common.base.BaseAction;
import cn.xshi.common.base.BaseBTreeGridEntity;
import cn.xshi.common.base.BaseResult;
import cn.xshi.common.entity.OauthAccountEntity;
import cn.xshi.common.util.StringUtil;
import cn.xshi.sys.form.SysDataDictionaryForm;
import cn.xshi.sys.model.SysDataDictionary;
import cn.xshi.sys.service.SysDataDictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
/**
 * @Desc 数据字典
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/sysDataDictionary")
@Api(value = "数据字典API",tags = "数据字典API",description = "数据字典API")
public class SysDataDictionaryController extends BaseAction {

	@Resource
	private SysDataDictionaryService sysDataDictionaryService;

	/**
	 * 查询数据字典列表
	 */
	@GetMapping(value="/list")
	@Auth(value = "/sysDataDictionary/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	@ApiOperation(value="查询数据字典列表", notes="查询数据字典列表")
	public BaseResult getSysDataDictionaryListByCondition(){
		List<BaseBTreeGridEntity> list = new ArrayList<BaseBTreeGridEntity>();
		SysDataDictionaryForm sysDataDictionaryForm = new SysDataDictionaryForm();
		List<SysDataDictionary> sysDataDictionaryList = sysDataDictionaryService.getSysDataDictionaryListByCondition(sysDataDictionaryForm);
		for(int i = 0; i < sysDataDictionaryList.size(); i++){
			SysDataDictionary sysDataDictionary = sysDataDictionaryList.get(i);
			BaseBTreeGridEntity BaseBTreeGridEntity = new BaseBTreeGridEntity();
			BaseBTreeGridEntity.setId(sysDataDictionary.getId());
			BaseBTreeGridEntity.setPid(sysDataDictionary.getParentId());
			BaseBTreeGridEntity.setText(sysDataDictionary.getName());
			BaseBTreeGridEntity.setContent(sysDataDictionary.getRemark());
//			BaseZTreeEntity.setIcon("../deng/images/icons/target.png");
			if(0== sysDataDictionary.getState()){
				BaseBTreeGridEntity.setTempObject("启用");
			}else{
				BaseBTreeGridEntity.setTempObject("禁用");
			}
			if(StringUtils.isEmpty(sysDataDictionary.getParentId()) || "0".equals(sysDataDictionary.getParentId())){
				BaseBTreeGridEntity.setIntegerappend("0");
			}else{
				BaseBTreeGridEntity.setIntegerappend("1");
			}
			list.add(BaseBTreeGridEntity);
		}
		return outStr(BaseBTreeGridEntity.buildTree(list));
	}

	/**
	 * 查询单个数据字典
	 * @param id
	 * @param request
	 */
	@ApiOperation(value="查询单个数据字典", notes="查询单个数据字典")
	@GetMapping(value="/get/{id}")
	@Auth(value = "/sysDataDictionary/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<SysDataDictionary> getSysDataDictionaryById(@PathVariable("id") String id, HttpServletRequest request){
		SysDataDictionary sysDataDictionary = sysDataDictionaryService.getSysDataDictionaryById(id);
		if(!StringUtil.isEmpty(sysDataDictionary.getCreateId())){
			OauthAccountEntity createBy = getAccount(sysDataDictionary.getCreateId());
			if(null != createBy){
				sysDataDictionary.setCreateBy(createBy.getName());
			}
		}
		if(!StringUtil.isEmpty(sysDataDictionary.getUpdateId())){
			OauthAccountEntity modifiedBy = getAccount(sysDataDictionary.getUpdateId());
			if(null != modifiedBy){
				sysDataDictionary.setModifiedBy(modifiedBy.getName());
			}
		}
		return outDataStr(sysDataDictionary);
	}

	/**
	 * 查询全部数据字典
	 * @return
	 */
	@GetMapping(value="/lists")
	@Auth(value = "/sysDataDictionary/lists",authenticationType = Auth.AuthorizationType.NOT_REQUIRED_LOGIN)
	@ApiOperation(value="查询全部数据字典", notes="查询全部数据字典")
	public BaseResult<List<SysDataDictionary>> getSysDataDictionaryById(){
		Map<String,Object> condition = new HashMap<>();
		List<SysDataDictionary> sysDataDictionaryList = sysDataDictionaryService.getSysDataDictionaryListAllByCondition(condition);
		return outDataStr(sysDataDictionaryList);
	}

	/**
	 * 添加
	 * @param sysDataDictionary
	 */
	@PostMapping(value="/add")
	@ApiOperation(value="创建单个数据字典", notes="创建单个数据字典")
	public BaseResult addSysDataDictionary(@RequestBody SysDataDictionary sysDataDictionary){
		int i = 0;
		if(null != sysDataDictionary){
			sysDataDictionary.setId(toUUID());
			i= sysDataDictionaryService.addSysDataDictionary(sysDataDictionary);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 修改
	 * @param sysDataDictionary
	 */
	@PutMapping(value="/update")
	@ApiOperation(value="编辑单个数据字典", notes="编辑单个数据字典")
	public BaseResult updateSysDataDictionary(@RequestBody SysDataDictionary sysDataDictionary){
		int i = 0;
		if(null != sysDataDictionary){
			i= sysDataDictionaryService.updateSysDataDictionary(sysDataDictionary);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 删除
	 * @param id
	 */
	@DeleteMapping(value="/delete")
	@ApiOperation(value="删除数据字典", notes="删除数据字典")
	public BaseResult delSysDataDictionary(String id){
		int i = 0;
		if(StringUtils.isNotBlank(id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("id",id.split(","));
			i= sysDataDictionaryService.delSysDataDictionary(condition);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 导出
	 * @param response
	 * @param id
	 */
	@ApiOperation(value="导出", notes="导出")
	@GetMapping(value = "/export")
	public void exportDictionary(HttpServletResponse response, String id) {
		sysDataDictionaryService.exportDictionary(response,id);
	}

	/**
	 * 导入
	 * @param request
	 * @return
	 */
	@ApiOperation(value="导入", notes="导入")
	@PostMapping(value = "/import")
	public BaseResult importDictionary(HttpServletRequest request,HttpServletResponse response) {
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;//转型为MultipartHttpRequest：
		MultiValueMap<String, MultipartFile> multipartFileMultiValueMap =  multipartRequest.getMultiFileMap();//获取所有文件
		Iterator<Map.Entry<String, List<MultipartFile>>> iterator = multipartFileMultiValueMap.entrySet().iterator();
		while (iterator.hasNext()) {
			Map.Entry<String, List<MultipartFile>> entry = iterator.next();
			for (MultipartFile multipartFile : entry.getValue()) {
				sysDataDictionaryService.importDictionary(request,response,multipartFile);
			}
		}
		return BaseResult.success();
	}
}

